#include <QtGui/QApplication>
#include <QtGui>
#include <QDebug>
#include <math.h>
#include <QFileDialog>

#include "SageVis/sagevis.h"

int main(int argc, char *argv[])
{
    //application setup
    QApplication app(argc, argv);
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));

    //---------------------------------------------------------------
    //GET AND SET SCENE DIMENSIONS
    float windowWidth = 1440.0;
    float windowHeight = 800.0;

    // check to see if user has given windowWidth and windowHeight on command line
    bool gotWidthAndHeight = false;
    if( argc == 3 )//if command line parameters given
    {
        //qDebug() " windowWidth = " << atoi( argv[3] ) << " windowHeight = " << atoi( argv[4] ) << endl;
//        windowWidth = atoi( argv[3]);
//        windowHeight = atoi( argv[4] ); //note- will be problem if not an integer

//        if( windowWidth < 0 || windowHeight < 0 || windowWidth > 1360.0*8 || windowHeight > 1020.0*4 )
//        {
//            qDebug() << "Input on command line is:  ./Go-SageVis gffFile fastaFile windowWidth windowHeight" << endl;
//            qDebug() << "where windowWidth and windowHeight are integers." << endl;
//            exit( 1 );
//        }

//        gotWidthAndHeight = true;
   }
    if( !gotWidthAndHeight )
    {
    #if defined(Q_OS_LINUX)
        windowWidth = 1360.0*2.0;//8160;
        windowHeight = 768.0*2.0;//2304;
    #endif
    #if defined(Q_WS_WIN)
        windowWidth = 1280.0;
        windowHeight = 750.0;
    #endif
    }

    //---------------------------------------------------------------
    //SETUP SCENE
    QGraphicsScene scene;
    scene.setSceneRect(-windowWidth/2.0, -windowHeight/2.0, windowWidth, windowHeight);//setup the scene dimensions
    scene.setItemIndexMethod(QGraphicsScene::NoIndex);//this index mode seems to be fastest
        //Other options:  QGraphicsScene::BspTreeIndex);
    //---------------------------------------------------------------

    //---------------------------------------------------------------
    //OPEN SAGEVIS and add to scene
    SageVis sageVis;
    sageVis.setPos(-windowWidth/2.0+10, -windowHeight/2.0+10);//Sagevis positioned in the upper left corner of scene
    scene.addItem( &sageVis );//add to scene

    //---------------------------------------------------------------
    //DEFINE path to the data folder
     //      Note: Not using right now, but may need to use someday
    QString pathToData = "../../../../SageVis/data"; //MAC
     #if defined(  Q_WS_WIN )
         pathToData = "./data";
     #endif
     #if defined(  Q_OS_LINUX )
         pathToData = "./data";
     #endif
         //Note:  not sure how this works when it is made into standalone application.

     //---------------------------------------------------------------
     //LAUNCH WIDGET
           //Note:  There are two options for launching widget
           //   option 1: supply just the widget type
                //in this case, a file browser menu will open and user must specify the data files
           //   option 2: supply the widget type and necessary data files to launch the widget
                //this is probably what will be used in SAGENext when user drags and drops files using SageNextPointer
     sageVis.launchVisWidget( "generegionviswidget" );

     //----------------------------------------------------------------
     //ADD SCENE TO VIEW
     QGraphicsView view(&scene);
     view.setRenderHints(QPainter::Antialiasing |  QPainter::SmoothPixmapTransform);//rendering setup
     view.setBackgroundBrush(QBrush(Qt::black));//black background

     view.setCacheMode(QGraphicsView::CacheBackground);//for efficiency
     view.setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);//most efficient mode based on initial test
        //Options:  QGraphicsView::MinimalViewportUpdate);//QGraphicsView::BoundingRectViewportUpdate);
     view.setDragMode(QGraphicsView::ScrollHandDrag);

     view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "SAGEVis"));
     view.resize(windowWidth,windowHeight);//view is sized to fit scene dimensions
     view.show();

     //----------------------------------------------------------------
     //LAUNCH TIMER -- MAYBE ELIMINATE ... ?
     //     Timer allows for updating and animation of objects in the scene
     //     Note:  not sure this timer is being used anymore ...
     QTimer timer;
     QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
     float ms = 1000.0;
     float frames = 60.0;
     timer.start( ms / frames );//runs at 60 frames per second

     //----------------------------------------------------------------
     //RUN
     return app.exec();
}
